package com.wtwd.campus.chain;

import com.wtwd.campus.common.RespCode;
import com.wtwd.campus.common.RespEntity;
import com.wtwd.campus.entity.DeviceInfo;
import com.wtwd.campus.service.DeviceInfoService;
import com.wtwd.campus.service.H5Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author mjy(占时只做瑞翼 家长 学生 设备 一对一)
 * 1.查询设备是否属于特定客户，属于特定客户特殊处理
 * @date 2022/04/19
 * @description
 */
@Component
public class VerifyH5DeviceBind extends H5BindDeviceChain {


    @Value("${h5.customerId.ryh5}")
    private Integer ryh5CustomerId;

    @Value("${h5.customerId.hbDxRyh5}")
    private Integer hbDxRyh5CustomerId;

    @Autowired
    private DeviceInfoService deviceInfoService;

    @Autowired
    private H5Service h5Service;

    @Override
    protected RespEntity H5BindDevice(Integer h5UserId, String imei, String studentName) {

        DeviceInfo device = deviceInfoService.getDeviceByImei(imei);
        if (device != null) {
            if (H5_DEVICE_TYPE.equals(device.getDeviceType())) {
                if (RY_CUSTOMER_ID.equals(device.getCustomerId()) || CH_CUSTOMER_ID.equals(device.getCustomerId())) { //用户设备一对一的绑定关系(湖南和教育的绑定关系为一对一的绑定关系)
                    List<Integer> deviceIdList = h5Service.deviceIdList(h5UserId);
                    if (deviceIdList != null && deviceIdList.size() > 0) {
                        return new RespEntity(RespCode.H5_USER_BIND_DEVICE);
                    }
                } else if (ryh5CustomerId.equals(device.getCustomerId())||hbDxRyh5CustomerId.equals(device.getCustomerId())) {//设备用户关系不存储在我们服务器,每次过来都会重新同步，故不不允许我们添加
                    return new RespEntity(RespCode.DEVICE_BINDED);
                }
            }
        }
        return new RespEntity(RespCode.SUCCESS);
    }

    public VerifyH5DeviceBind() {
        this.level = H5BindDeviceChain.verifyDeviceBind;
    }


}
